/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ohos.speechutilsdemo;

import com.ohos.speechutilsdemo.slice.MainAbilitySlice;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;
import ohos.bundle.IBundleManager;

import ee.ioc.phon.ohos.speechutils.utils.Define;

/**
 * 主页面
 *
 * @since 2021-04-06
 */
public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        /**
         * 隐藏状态栏 ,沉浸式状态栏
         */
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        requestPermissionsFromUser(new String[]{Define.MICROPHONE, Define.SHORTCUTS}, 0);
        if (verifySelfPermission(Define.SHORTCUTS) != IBundleManager.PERMISSION_DENIED) {
            // 应用权限未被授予
            if (canRequestPermission(Define.SHORTCUTS)) {
                // 验证是否可以申请弹窗授权（首次申请或者用户未选择禁止且不再提示）
                requestPermissionsFromUser(new String[]{Define.SHORTCUTS}, Define.FUYI);
            } else {
                // 显示应用权限需要权限的理由，提示用户进入设置授权
                new ToastDialog(this).setText("请前往设置授予录音权限").setDuration(Define.ERQIAN).show();
            }
        } else {
            new ToastDialog(this).setText("录音权限已被授予").setDuration(Define.ERQIAN).show();
        }
        if (verifySelfPermission(Define.MICROPHONE) != IBundleManager.PERMISSION_DENIED) {
            // 应用权限未被授予
            if (canRequestPermission(Define.MICROPHONE)) {
                // 验证是否可以申请弹窗授权（首次申请或者用户未选择禁止且不再提示）
                requestPermissionsFromUser(new String[]{Define.MICROPHONE}, Define.FUYI);
            } else {
                // 显示应用权限需要权限的理由，提示用户进入设置授权
                new ToastDialog(this).setText("请前往设置授予麦克风权限").setDuration(Define.ERQIAN).show();
            }
        } else {
            new ToastDialog(this).setText("麦克风权限已被授予").setDuration(Define.ERQIAN).show();
        }
    }
}
